package com.qf.shop.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.shop.entity.Type;
import com.qf.shop.service.TypeService;
import com.qf.shop.service.impl.TypeServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet( "/type")//返回json类型字符串
public class TypeController extends BaseController {
   public String getAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
       request.setCharacterEncoding("utf-8");
       response.setContentType("text/html;charset=utf-8");
      //1.业务层调用查询所有商品类型
       TypeService typeService = new TypeServiceImpl();
       //2.返回集合，集合转换成json字符串
       List<Type> allTypes = typeService.getAllTypes();
       ObjectMapper objectMapper = new ObjectMapper();
       String s = objectMapper.writeValueAsString(allTypes);
       //3.响应给客户端
       return s;
   }
}
